<?php
class Data_gudang_model extends CI_Model {
	private $primary_key = 'id_bahan';
	private $table_name = 'bahan_gudang';

	function __construct(){
		parent::__construct();
	}
	
	function getAll() {
		$this->db->select('*');
		$this->db->from($this->table_name);
		$this->db->join('satuan', 'bahan_gudang.id_satuan = satuan.id_satuan');
		$this->db->join('kategori_bahan', 'bahan_gudang.id_kategori_bahan = kategori_bahan.id_kategori_bahan' );
		$this->db->order_by($this->primary_key, "asc"); 
		return $this->db->get();
	}
	
	function get_bahan_masuk_by_no() {
		$query = "SELECT * FROM `bahan_masuk` INNER JOIN bahan_gudang on bahan_masuk.id_bahan=bahan_gudang.id_bahan WHERE no_transaksi=(SELECT MAX(no_transaksi) FROM `bahan_masuk`)";
//		echo $query;die();
		return $this->db->query($query);
	}
	
	function getSatuan($id) {
		$this->db->select('*');
		$this->db->where_not_in('id_satuan', $id);
		$this->db->from('satuan');
		return $this->db->get();
	}
	
	function getKategori($id) {
		$this->db->select('*');
		$this->db->where_not_in('id_kategori_bahan', $id);
		$this->db->from('kategori_bahan');
		return $this->db->get();
	}
	
	function get_by_id($id){
		$this->db->select('*');
		$this->db->from($this->table_name);
		$this->db->join('satuan', 'bahan_gudang.id_satuan = satuan.id_satuan');
		$this->db->join('kategori_bahan', 'bahan_gudang.id_kategori_bahan = kategori_bahan.id_kategori_bahan' );
		$this->db->where($this->primary_key,$id);
		$this->db->order_by($this->primary_key, "asc"); 
		return $this->db->get();
	}	
	
	function save($bahan){
		$this->db->insert($this->table_name,$bahan);
	}
	
	function update($id,$bahan){
		$this->db->where($this->primary_key,$id);
		$this->db->update($this->table_name,$bahan);
	}
	
	function delete($id){
		$this->db->where($this->primary_key,$id);
		$this->db->delete($this->table_name);
	}
	
	function getName() {
		$this->db->select('nama_bahan');
		return $this->db->get($this->table_name);
	}
	function get_id_bahan($nama) {
		$this->db->select($this->primary_key);
		$this->db->like('nama_bahan',$nama, 'none');
		$this->db->limit(1);
		return $this->db->get($this->table_name);
	}
	//bahan masuk model
	function getTemp_bahan_masuk(){
		$this->db->select('*');
		$this->db->join($this->table_name, 'bahan_gudang.id_bahan = temp_bahan_masuk.id_bahan');
		return $this->db->get('temp_bahan_masuk');
	}
	function getTemp_jumlah_by_id($id){
		$this->db->where($this->primary_key,$id);
		return $this->db->get('temp_bahan_masuk');
	}
	function saveTemp_bahan_masuk($bahan_masuk, $jumlah){
		$total = $bahan_masuk['jumlah']+$jumlah;
		$query="REPLACE INTO temp_bahan_masuk VALUES('', '$bahan_masuk[id_bahan]', '$bahan_masuk[tanggal]', '$total', $bahan_masuk[no_transaksi])";
		$this->db->query($query);
	}
	function updateTemp_bahan_masuk($id,$bahan_masuk){
		$this->db->where($this->primary_key,$id);
		$this->db->update('temp_bahan_masuk',$bahan_masuk);
	}
	function deleteTemp_bahan_masuk($id){
		$this->db->where($this->primary_key,$id);
		$this->db->delete('temp_bahan_masuk');
	}
	function save_bahan_masuk(){
		$query = "insert into bahan_masuk(no_transaksi, id_bahan, tanggal, jumlah) select no_transaksi, id_bahan, tanggal, jumlah from temp_bahan_masuk";
		$this->db->query($query);
	}
	function total_stok(){
		$query = "SELECT temp_bahan_masuk.id_bahan, stok+jumlah as total FROM `bahan_gudang` inner join `temp_bahan_masuk` on bahan_gudang.id_bahan=temp_bahan_masuk.id_bahan";
//		echo $query;die();
		return $this->db->query($query);
	}
	function truncateTemp_bahan_masuk(){
		$this->db->truncate('temp_bahan_masuk');
	}
	function get_last(){
		$query = "SELECT no_transaksi FROM bahan_masuk WHERE id = (SELECT MAX(id) FROM bahan_masuk) LIMIT 1";
		return $this->db->query($query);
	}
	
	function cek_id($id) {
		$this->db->like($this->primary_key,$id);
		return $this->db->get($this->table_name);
	}
	function cek_name($name) {
	$this->db->like('nama_bahan',$name,'none');
	return $this->db->get($this->table_name);
	}
	function count_all(){
	return $this->db->count_all($this->table_name);
	}
}